package com.turing.post.cardmanagement.util;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.turing.post.util.PCPSUtil;

/**
 * 制卡控制
 * 
 * @author kz
 * @date 2012-12-17
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "MakeCardControl")
public class MakeCardControl implements Serializable {

	public MakeCardControl() {
		super();
	}

	public MakeCardControl(String batchNo, String issueBranchNo,
			BigInteger cardFaceId, String cardSeqNo, Integer noInCardCount) {
		this.batchNo = batchNo;
		this.issueBranchNo = issueBranchNo;
		this.cardFaceId = cardFaceId;
		this.cardSeqNo = cardSeqNo;
		this.noInCardCount = noInCardCount;
	}

	/**
	 * @枚举声明
	 */

	/**
	 * 状态-卡号产生中
	 */
	public final static String STATUS_MAKING = "0";

	/**
	 * 状态-产生成功，未下载
	 */
	public final static String STATUS_WAITDOWNLOAD = "1";

	/**
	 * 状态-已下载，待制实体卡
	 */
	public final static String STATUS_DOWNLOAD = "2";

	/**
	 * 状态-制卡成功，已入库
	 */
	public final static String STATUS_INSTOCK = "3";

	/**
	 * 状态-制卡不成功，作废
	 */
	public final static String STATUS_CANCEL = "4";

	/**
	 * 状态-制卡成功,部分入库
	 */
	public final static String STATUS_PARTIN = "5";

	/**
	 * 制卡批次号
	 */
	private String batchNo;
	/**
	 * 制卡原因
	 */
	private String reason;
	/**
	 * 发卡商NO
	 */
	private String issueCompanyNo;
	/**
	 * 制卡机构号
	 */
	private String issueBranchNo;
	/**
	 * 制卡人
	 */
	private BigInteger operNo;

	/**
	 * 制卡起始时间
	 */
	private Date startTime;

	/**
	 * 制卡终止时间
	 */
	private Date endTime;

	/**
	 * 制卡商ID
	 */
	private BigInteger cardManufactureId;
	//
	// /**
	// * 卡片种类
	// */
	// private BigInteger cardClassicId;
	//
	// /**
	// * 卡片面值
	// */
	// private BigInteger cardParValueId;

	/**
	 * 卡面图样ID
	 */
	private BigInteger cardFaceId;
	/**
	 * 代发卡Bin
	 */
	private String proxyIssue;

	/**
	 * 制卡张数
	 */
	private Integer cardCount;

	/**
	 * 有效期
	 */
	private Integer expired;

	/**
	 * 有效期单位
	 */
	private String expiredUnit;

	/**
	 * 有效期算法
	 */
	private String expiredAlgorithm;

	/**
	 * 状态
	 */
	private String status;

	/**
	 * 制卡文件长度
	 */
	// private Integer cardFileLength;
	/**
	 * 制卡文件内容
	 */
	// private byte[] cardFileContent;
	private BigInteger cardFileContentId;

	/**
	 * 已入库张数
	 */
	private Integer alreadyInStockCount;

	/**
	 * 提示信息
	 */
	private String hintInfo;

	/**
	 * 单号
	 */
	private String orderNumber;
	/**
	 * 介质类型
	 */
	private String mediaType;

	private String cardSeqNo;

	private Integer noInCardCount;

	@Id
	@Column(name = "BATCHNO", nullable = false)
	public String getBatchNo() {
		return batchNo;
	}

	public void setBatchNo(String batchNo) {
		this.batchNo = batchNo;
	}

	@Column(name = "REASON", nullable = true)
	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	@Column(name = "ISSUECOMPANYNO", nullable = false)
	public String getIssueCompanyNo() {
		return issueCompanyNo;
	}

	public void setIssueCompanyNo(String issueCompanyNo) {
		this.issueCompanyNo = issueCompanyNo;
	}

	@Column(name = "ISSUEBRANCHNO", nullable = false)
	public String getIssueBranchNo() {
		return issueBranchNo;
	}

	public void setIssueBranchNo(String issueBranchNo) {
		this.issueBranchNo = issueBranchNo;
	}

	@Column(name = "OPERNO", nullable = false)
	public BigInteger getOperNo() {
		return operNo;
	}

	public void setOperNo(BigInteger operNo) {
		this.operNo = operNo;
	}

	@Column(name = "STARTTIME", nullable = false)
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	@Column(name = "ENDTIME", nullable = true)
	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	@Column(name = "CARDMANUFACTUREID", nullable = false)
	public BigInteger getCardManufactureId() {
		return cardManufactureId;
	}

	public void setCardManufactureId(BigInteger cardManufactureId) {
		this.cardManufactureId = cardManufactureId;
	}

	// @Column(name = "CARDCLASSICID", nullable = false)
	// public BigInteger getCardClassicId() {
	// return cardClassicId;
	// }
	//
	// public void setCardClassicId(BigInteger cardClassicId) {
	// this.cardClassicId = cardClassicId;
	// }
	//
	// @Column(name = "CARDPARVALUEID", nullable = false)
	// public BigInteger getCardParValueId() {
	// return cardParValueId;
	// }
	//
	// public void setCardParValueId(BigInteger cardParValueId) {
	// this.cardParValueId = cardParValueId;
	// }
	@Column(name = "cardFaceId")
	public BigInteger getCardFaceId() {
		return cardFaceId;
	}

	public void setCardFaceId(BigInteger cardFaceId) {
		this.cardFaceId = cardFaceId;
	}

	@Column(name = "proxyIssue")
	public String getProxyIssue() {
		return proxyIssue;
	}

	public void setProxyIssue(String proxyIssue) {
		this.proxyIssue = proxyIssue;
	}

	@Column(name = "CARDCOUNT", nullable = false)
	public Integer getCardCount() {
		return cardCount;
	}

	public void setCardCount(Integer cardCount) {
		this.cardCount = cardCount;
	}

	@Column(name = "EXPIRED", nullable = false)
	public Integer getExpired() {
		return expired;
	}

	public void setExpired(Integer expired) {
		this.expired = expired;
	}

	/**
	 * 有效期单位 - 年
	 */
	public static final String EXPIRED_UNIT_YEAR = "Y";
	/**
	 * 有效期单位 - 月
	 */
	public static final String EXPIRED_UNIT_MONTH = "M";
	/**
	 * 有效期单位 - 日
	 */
	public static final String EXPIRED_UNIT_DAY = "D";

	@Column(name = "EXPIREDUNIT", nullable = false)
	public String getExpiredUnit() {
		return expiredUnit;
	}

	public void setExpiredUnit(String expiredUnit) {
		this.expiredUnit = expiredUnit;
	}

	/**
	 * 对年对月对日加
	 */
	public static final String EXPIRED_ALGORITHM_ADD_BY_UNIT = "0";

	/**
	 * 对加后的月底
	 */
	public static final String EXPIRED_ALGORITHM_ADD_BY_UNIT_END_MONTH = "1";

	/**
	 * 对加后的季末
	 */
	public static final String EXPIRED_ALGORITHM_ADD_BY_UNIT_END_SEASON = "2";

	/**
	 * 激活时提示对加后日期，可由激活人员修改
	 */
	public static final String EXPIRED_ALGORITHM_BY_USER = "3";

	@Column(name = "EXPIREDALGORITHM", nullable = false)
	public String getExpiredAlgorithm() {
		return expiredAlgorithm;
	}

	public void setExpiredAlgorithm(String expiredAlgorithm) {
		this.expiredAlgorithm = expiredAlgorithm;
	}

	@Column(name = "STATUS", nullable = false)
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	// @Column(name = "CARDFILELENGTH", nullable = false)
	// public Integer getCardFileLength() {
	// return cardFileLength;
	// }
	//
	// public void setCardFileLength(Integer cardFileLength) {
	// this.cardFileLength = cardFileLength;
	// }

	@Column(name = "CARDFILECONTENTID", nullable = false)
	public BigInteger getCardFileContentId() {
		return cardFileContentId;
	}

	public void setCardFileContentId(BigInteger cardFileContentId) {
		this.cardFileContentId = cardFileContentId;
	}

	@Column(name = "ALREADYINSTOCKCOUNT", nullable = false)
	public Integer getAlreadyInStockCount() {
		return alreadyInStockCount;
	}

	public void setAlreadyInStockCount(Integer alreadyInStockCount) {
		this.alreadyInStockCount = alreadyInStockCount;
	}

	@Column(name = "hintInfo")
	public String getHintInfo() {
		return hintInfo;
	}

	public void setHintInfo(String hintInfo) {
		this.hintInfo = hintInfo;
	}

	@Column(name = "orderNumber")
	public String getOrderNumber() {
		return orderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}

	@Column(name = "mediaType")
	public String getMediaType() {
		return mediaType;
	}

	public void setMediaType(String mediaType) {
		this.mediaType = mediaType;
	}

	@Transient
	public String getCardSeqNo() {
		return cardSeqNo;
	}

	public void setCardSeqNo(String cardSeqNo) {
		this.cardSeqNo = cardSeqNo;
	}

	@Transient
	public Integer getNoInCardCount() {
		return noInCardCount;
	}

	public void setNoInCardCount(Integer noInCardCount) {
		this.noInCardCount = noInCardCount;
	}

	/**
	 * 计算有效期
	 * 
	 * @return
	 */
	private Date calcExpiredByUnit(Date curTime) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(curTime);
		if (PCPSUtil.compareObject(EXPIRED_UNIT_YEAR, getExpiredUnit()))
			cal.add(java.util.Calendar.YEAR, getExpired());
		else if (PCPSUtil.compareObject(EXPIRED_UNIT_MONTH, getExpiredUnit()))
			cal.add(java.util.Calendar.MONTH, getExpired());
		else if (PCPSUtil.compareObject(EXPIRED_UNIT_DAY, getExpiredUnit()))
			cal.add(java.util.Calendar.DATE, getExpired());
		return cal.getTime();
	}

	/**
	 * 根据参数及当前时间计算有效期
	 * 
	 * @param curTime
	 * @return
	 */
	public Date calcExpired(Date curTime, Date userTime) {
		if (PCPSUtil.compareObject(EXPIRED_ALGORITHM_BY_USER,
				getExpiredAlgorithm()))
			return userTime;
		curTime = calcExpiredByUnit(curTime);
		if (PCPSUtil.compareObject(EXPIRED_ALGORITHM_ADD_BY_UNIT,
				getExpiredAlgorithm()))
			return curTime;
		else if (PCPSUtil.compareObject(
				EXPIRED_ALGORITHM_ADD_BY_UNIT_END_MONTH, getExpiredAlgorithm())) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(curTime);
			calendar.set(Calendar.DAY_OF_MONTH, calendar
					.getActualMaximum(Calendar.DAY_OF_MONTH));
			return calendar.getTime();
		} else if (PCPSUtil
				.compareObject(EXPIRED_ALGORITHM_ADD_BY_UNIT_END_SEASON,
						getExpiredAlgorithm())) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(curTime);
			int month = calendar.get(Calendar.MONTH);
			if (month <= 2)
				calendar.set(Calendar.MONTH, 2);
			else if (month <= 5)
				calendar.set(Calendar.MONTH, 5);
			else if (month <= 8)
				calendar.set(Calendar.MONTH, 8);
			else
				calendar.set(Calendar.MONTH, 11);
			calendar.set(Calendar.DAY_OF_MONTH, calendar
					.getActualMaximum(Calendar.DAY_OF_MONTH));
			return calendar.getTime();
		}
		return null;
	}
}